//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if !defined(_LOADMISC_H_)
#define _LOADMISC_H_

#ifdef __cplusplus
extern "C" {
#endif

typedef void (__cdecl *ENTRYPOINT)(...);

#define RoundUp(val, align) \
    ( ((Address)(val) + (Address)(align) - 1) & ~((Address)(align) - 1) )

int main(void);
void C_main(void);

void PortInit(unsigned long Port);
unsigned char ReadByte();
void WriteByte(unsigned char c);

void copy(unsigned long *dst, unsigned long *src, unsigned long size);
void ReadBlock(unsigned char *buffer, long size);
void WriteBlock (unsigned long where, unsigned char *Buffer, long Size);
unsigned long ReadWord(void);
unsigned long ReadWordNoWait(void);
void WriteWord(unsigned long x);
void debug(unsigned char c);
void debugw(unsigned long x);
void mdump(unsigned long addr, int size);
void Delay(unsigned long uSec);
void WriteNumber(unsigned long val);
void WriteNumber2(unsigned long uVal);
void cprint(const char * pStrSend);
// flash baseaddr on normal mode
#define HwFlashBase         0
#define HwFlashSectorSize   0x00020000/*128K*/
//BUGBUG: reserve one sector size for loader
#define HwFlashOffset       HwFlashSectorSize

#define WRITE_MAGIC    0x3BADBAD3

//==========================================================================
//flash operator functions
//==========================================================================
int FlashEraseSector (unsigned long FlashBase, long Offset);
int FlashWriteBuffer (unsigned long FlashBase, long Offset, unsigned char *Buffer, long BufferSize);
int FlashReadBuffer  (unsigned long FlashBase, long Offset, unsigned char *Buffer, long BufferSize);

#define queryflag   '?'
#define startflag   '<'
#define endflag     '>'
#define noteflag    '!'
#define debugflag   'x'
#define writeflag   'w'
#define eraseflag   'e'

typedef struct {
    unsigned long Virt;
    unsigned long Phys;
    unsigned long Size;
    unsigned long Cached;
} MEMORY_MAP_ENTRY, *PMEMORY_MAP_ENTRY;
MEMORY_MAP_ENTRY mmap[];

#ifdef __cplusplus
}
#endif

#endif // _LOADMISC_H_
